package com.basic.modules.ykf.controller;

import java.util.Arrays;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.basic.common.annotation.StringTrim;
import com.basic.common.exception.RRException;
import com.basic.common.utils.ParamsModel;
import com.basic.common.validator.ValidatorUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.basic.modules.ykf.entity.YkfVideoManageEntity;
import com.basic.modules.ykf.service.YkfVideoManageService;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.R;



/**
 * 视频管理
 *
 * @author maoheng
 * @email 15738859597@163.com
 * @date 2024-05-13 18:21:12
 */
@RestController
@RequestMapping("ykf/videoManage")
public class YkfVideoManageController {

    @Autowired
    private YkfVideoManageService ykfVideoManageService;

    /**
     * 列表-分页
     */
    @StringTrim
    @PostMapping("/page")
    public R list(@RequestBody ParamsModel<YkfVideoManageEntity> params){
        PageUtils page = ykfVideoManageService.queryPage(params);
        return R.ok(page);
    }


    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody YkfVideoManageEntity params){
        if (ObjectUtil.isEmpty(params) || ObjectUtil.isEmpty(params.getVideoManageId())) {
            throw new RRException("参数缺失");
        }
        return R.ok(ykfVideoManageService.getById(params.getVideoManageId()));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody YkfVideoManageEntity ykfVideoManage){
        ValidatorUtils.validateEntity(ykfVideoManage);
        validVideo(ykfVideoManage);
		ykfVideoManageService.save(ykfVideoManage);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody YkfVideoManageEntity ykfVideoManage){
        ValidatorUtils.validateEntity(ykfVideoManage);
        validVideo(ykfVideoManage);
		ykfVideoManageService.updateById(ykfVideoManage);
        return R.ok();
    }

    private void validVideo(YkfVideoManageEntity ykfVideoManage) {
        if (StrUtil.equals(ykfVideoManage.getType(), "FILE")) {
            //取阿里云OSS文件第一帧
            ykfVideoManage.setVideoFirstImg(ykfVideoManage.getUrl() + "?x-oss-process=video/snapshot,t_0,f_jpg,w_0,h_0,m_fast,ar_auto");
        } else {
            ykfVideoManage.setVideoFirstImg("");
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] videoManageIds){
		ykfVideoManageService.removeByIds(Arrays.asList(videoManageIds));
        return R.ok();
    }

}
